

/*
* Copyright @ 2017 com.iflysse.trains
* crm 下午11:14:37
* All right reserved.
*
*/
	
package com.qf.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.qf.dto.CrmDepartmentDto;
import com.qf.dto.CrmPostDto;
import com.qf.pojo.CrmDepartment;
import com.qf.pojo.CrmPost;
import com.qf.pojo.PagePost;
import com.qf.service.CrmDepartmentService;
import com.qf.service.CrmPostService;

/**
* @desc: crm
* @descripe:XXXX
* @author: 周锐
* @createTime: 2017年7月24日 下午11:14:37
* @history:
* @version: v1.0

*/
@Controller
public class CrmPostServlet {
	@Autowired
	@Qualifier("crmPostService")
	private CrmPostService ps;
	@Autowired
	@Qualifier("crmDepartmentService")
	private CrmDepartmentService cs;
	 
	/**
	* @param ps the ps to set
	*/
	
	public void setPs(CrmPostService ps) {
	
		this.ps = ps;
	}

	
	/**
	* @param cs the cs to set
	*/
	
	public void setCs(CrmDepartmentService cs) {
	
		this.cs = cs;
	}

	@RequestMapping("/addPost.do")
	    public ModelAndView addPost(HttpServletRequest request,HttpServletResponse response){
		ModelAndView mv=new ModelAndView();
		String depId_ = request.getParameter("depId");
		Long depId=Long.valueOf(depId_);
		CrmDepartment crmDepartment=new CrmDepartment();
		crmDepartment.setDepId(depId);
	    String postName = request.getParameter("postName");
	    CrmPost post = new CrmPost();
	    post.setCrmDepartment(crmDepartment);
	    post.setPostName(postName);
	    ps.addPost(post);
	    mv.setViewName("findPagePost.do");
			return mv;
		  
	  }
	@RequestMapping("/findPagePost.do")
	 public ModelAndView findPagePost(HttpServletRequest request,HttpServletResponse response){
		ModelAndView mv=new ModelAndView();
		String page_ = request.getParameter("page");
		int page=1;
		int size=3;
		if(page_!=null){
			page=Integer.parseInt(page_);
		}
		PagePost pagePost = ps.findbyPage(page, size);
		mv.addObject("pagePost", pagePost);
		mv.setViewName("/pages/post/listPost.jsp");
		return mv;
}
	@RequestMapping("/preUpdatePost.do") 
	 public ModelAndView preUpdatePost(HttpServletRequest request,HttpServletResponse response){
		 ModelAndView mv=new ModelAndView();
		 String postId = request.getParameter("postId");
		 CrmPost post = ps.findbyId(Long.valueOf(postId));
		 List<CrmDepartmentDto> deptList = cs.findAllDepartment();
		 mv.addObject("post", post);
		 mv.addObject("deptList", deptList);
		 mv.setViewName("/pages/post/editPost.jsp");
		return mv;
	 }
	@RequestMapping("/UpdatePost.do") 
	 public ModelAndView UpdatePost(HttpServletRequest request,HttpServletResponse response){
		ModelAndView mv=new ModelAndView();
		String postId_ = request.getParameter("postId");
		String depId_ = request.getParameter("depId");
		String postName = request.getParameter("postName");
		Long postId=Long.valueOf(postId_);
		Long depId=Long.valueOf(depId_);
		CrmDepartment crmDepartment=new CrmDepartment();
		crmDepartment.setDepId(depId);
		CrmPost post = ps.findbyId(postId);
		post.setCrmDepartment(crmDepartment);
		post.setPostName(postName);
		ps.updatePost(post);
		mv.setViewName("findPagePost.do");
		return mv;
		
	}
	@RequestMapping("/allPost.do") 
	 public @ResponseBody List<CrmPostDto> allPost(HttpServletRequest request,HttpServletResponse response){
		String depId_=request.getParameter("depId");
		Long depId=Long.valueOf(depId_);
		List<CrmPostDto> posts = ps.findbyAllPost(depId);
		return posts;
		
	}
	 }

	